package com.lz.football.module.club
{
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ClubEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.buffer.vo.BufferVo;
	import com.liangzhong.model.club.ClothAttributeAdd;
	import com.liangzhong.model.club.vo.ClothVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.lz.football.module.club.item.BaseItem;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.HScroll;
	/**
	 * 球衣 
	 * @author Administrator
	 * 
	 */	
	public class ClubShirtView extends BaseView
	{
		private var _hscroll:HScroll;
		/**
		 * 
		 */
		private var _target:Sprite;
		/**
		 * 
		 */
		private var shirtEff:MovieClip;
		/**
		 *球衣人物 
		 */
		private var _clothMan:ClothManContain;
		/**
		 *球衣名称 
		 */
		private var clothName:TextField;
		/**
		 *球衣品质 
		 */
		private var quality:TextField;
		/**
		 *球衣属性 
		 */
		private var attribute:TextField;
		/**
		 *保存按钮 
		 */
		private var saveBtn:SimpleButton;
		/**
		 *选择了球衣 
		 */
		private var _selectItem:BaseItem
		
		public function ClubShirtView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
//			_windowBg = Reflection.createInstance("com.lz.modul.club.shirtBg");
//			this.addChild(_windowBg);
			symbolSkin = Reflection.createInstance("com.lz.modul.club.shirt");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.clothBg;
			shirtEff = symbolSkin.shirtEff;
			clothName  = symbolSkin.clothName;
			quality = symbolSkin.quality;
			attribute = symbolSkin.attribute;
			saveBtn = symbolSkin.saveBtn;
			attribute.htmlText = "";
			isDrag = true;
			isCloseBtn = true;
			super.initUI();
			moveClose(512,3);
			
			initData();
		}
		/**
		 *初始化数据 
		 * 
		 */
		private function initData():void
		{
			if(_target == null)
				_target = new Sprite();
			_target.x = 0;
			var i:int;
			var arr:Array = AppData.getInstance().clubDatamanager.trikotList;
			var len:int = arr.length;
			var item:BaseItem;
			var clothVo:GoodsVo //= AppData.getInstance().clubDatamanager.getClothVo(1);
			var p:int;
			for(i= 0;i < len;i++)
			{
				clothVo = AppData.getInstance().goodsManager.getTrikotById(arr[i]);
				item = new BaseItem();
				item.initUI("com.lz.commonUI.ItemFrame");
				item.callBack = selectCloth;
				_target.addChild(item);
				item.setImageSorce(clothVo.bitmapData);
				item.data = clothVo;
				item.tips = clothVo.tipsInfo//buff.bufferTips;
				item.x = i * 58;
				if(AppData.getInstance().clubDatamanager.clubInfo.homekot == clothVo.pid)
				{
					selectCloth(item);
					p = i;
				}
			}
			for(i ;i < 8;i++)
			{
				item = new BaseItem();
				item.initUI("com.lz.commonUI.ItemFrame");
				_target.addChild(item);
				item.x = i * 58;
			}
			initScroll();
			if(p - 7 > 0)
			{
				_target.x = 58* (7 - p);
			}else
			{
				_target.x = 0;
			}
			_hscroll.current = 0;
			_hscroll.upData();
		}
		/**
		 *初始化流动条 
		 * 
		 */
		private function initScroll():void
		{
			if(_hscroll != null)return;
			_hscroll = new HScroll();
			_hscroll.setBtn(symbolSkin._leftBtn,symbolSkin._rightBtn);
			_hscroll.defaultContain = symbolSkin._default;
			_hscroll.setWidth(464);
			_hscroll.defaultTarget = _target;
			_hscroll.speed = 58;
			_hscroll.delayedTime = 0.3;
		}
		/**
		 *选择了球衣 
		 * 
		 */
		private function selectCloth($item:BaseItem):void
		{
			if(_selectItem != null)
			{
				_selectItem.select = false;
			}
			_selectItem = $item;
			_selectItem.select = true;
			var clothVo:GoodsVo = _selectItem.data as GoodsVo;
			if(_clothMan == null)
			{
				_clothMan = new ClothManContain();
				
			}
			shirtEff.addChild(_clothMan);
			_clothMan.urlById(clothVo.pid);
			setDate(clothVo);
		}
		private function setDate(clothVo:GoodsVo):void
		{
			clothName.text = clothVo.name;
			var arr:Array = ["山寨","廉价","普通","精致","昂贵","顶级","逆天","神性"];
			quality.text = arr[clothVo.color -1] + "";
			var buff:BufferVo = AppData.getInstance().bufferManager.getBufferById(clothVo.buffid);
			attribute.htmlText = ClothAttributeAdd.attributeToStr(buff);/*buff.bufferTips*/;
		}
		override public function addEvtListener():void
		{
			super.addEvtListener();
			saveBtn.addEventListener(MouseEvent.CLICK,onSaveHandler);
			
		}
		
		/**
		 *保存队徽 
		 * @param event
		 * 
		 */
		protected function onSaveHandler(event:MouseEvent):void
		{
//			trace(AppData.getInstance().time);
//			trace(AppData.getInstance().clubDatamanager.clubInfo.time);
//			trace(AppData.getInstance().clubDatamanager.clubInfo.serverTime);
			
			
			
			if(_selectItem == null)
			{
				Alert.showErrorTip("请选择更换的球衣");
				return;
			}
			saveBtn.removeEventListener(MouseEvent.CLICK,onSaveHandler);
			AppEventDispatcher.getInstance().dispatchEvent(new ClubEvent(ClubEvent.CHANGESHIRT,{id:(_selectItem.data as GoodsVo).pid}));
			
		}
	
		/**
		 *更换球衣返回 
		 * 
		 */
		public function getChangeShirt():void
		{
			saveBtn.addEventListener(MouseEvent.CLICK,onSaveHandler);
		}
		/**
		 *获得一件新球衣 
		 * 
		 */
		public function getOneShirt($obj:Object):void
		{
			if(_target == null)return;
			var item:BaseItem;
			while(_target.numChildren)
			{
				item = _target.removeChildAt(0) as BaseItem;
				item.destroyView();
				item = null;
			}
			initData()
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			saveBtn.removeEventListener(MouseEvent.CLICK,onSaveHandler);
			
		}
		override public function destroyView():void
		{
			
			removeEvtListener();
			
			_hscroll.destroy();
			var item:BaseItem;
			while(_target.numChildren)
			{
				item = _target.removeChildAt(0) as BaseItem;
				item.destroyView();
				item = null;
			}
			if(_clothMan)
			{
				_clothMan.destroyView();
			}
			_clothMan = null;
			_selectItem = null;
			
			super.destroyView();
		}
	}
}
//override public function initUI(view:Object=null):void
//{
//	
//}
//override public function addEvtListener():void
//{
//	
//}
//override public function removeEvtListener():void
//{
//	
//}
//override public function destroyView():void
//{
//	
//}